package com.indeep.user.mapper.po;

import com.baomidou.mybatisplus.annotation.*;

import com.indeep.user.emun.AppUserIdentity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Laixin
 * @description app_user
 * @date 2023-08-29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("app_user")
public class AppUserPO extends BaseEntity implements Serializable {


    private static final long serialVersionUID = 3278955316252156410L;
    /**
     * 用户id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 随机的用户id，用来展示（10位，首位不为0）
     */
    private Integer ruid;

    /**
     * 昵称
     */
    private String username;

    /**
     * 身份
     * 0——用户
     * 1——vip
     * 2——kol
     */
    private Integer identity;
    
    /**
     * 微信openid
     */
    private String vxOpenid;

    /**
     * 手机号码
     */
    private String tel;

    /**
     * 头像url
     */
    private String avatar;

    /**
     * 背景图片url
     */
    private String backgroundPicture;

    /**
     * 0——正常
     * 1——封禁
     * 2——注销
     */
    private Integer status;

    /**
     * 关注数
     */
    private Integer follow;

    /**
     * 粉丝数
     */
    private Integer fans;

    /**
     * 地区
     */
    private String area;

    /**
     * 生日
     */
    private Date birthday;

    /**
     * 性别
     * 1——男
     * 2——女
     */
    private Integer sex;

    /**
     * 个性签名
     */
    private String signature;

    /**
     * 最后一次登录的时间
     */
    private Date lastLoginDatetime;

    /**
     * 创建时间-创建时自动填充
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 更新时间-创建与修改时自动填充
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    public AppUserPO(UpdateAppUserDTO dto) {
        this.id = dto.getId();
        this.username = dto.getUsername();
        this.tel = dto.getTel();
        Sexes sex = Sexes.getByStr(dto.getSex());
        if (sex != null) this.sex = sex.index;
        AppUserIdentity identity1 = AppUserIdentity.getByStr(dto.getIdentity());
        if (identity1 != null) this.identity = identity1.index;
        this.avatar = dto.getAvatar();
        this.backgroundPicture = dto.getBackgroundPicture();
    }

    /**
     * 将DTO的信息同步到DO中
     *
     * @param basicUserInfo 客户端传递的DTO
     */
    public void addBasicInfo(UpdateBasicUserInfoDTO basicUserInfo) {
        if (basicUserInfo == null) return;
        Date birthday = null;
        try {
            birthday = new SimpleDateFormat("yyyy-MM-dd").parse(basicUserInfo.getBirthday());
        } catch (ParseException | NullPointerException ignore) {
        }
        if (basicUserInfo.getBirthday() != null) this.birthday = birthday;
        if (basicUserInfo.getCity() != null) this.area = basicUserInfo.getCity();
        if (basicUserInfo.getSex() != null) this.sex = Sexes.getByStr(basicUserInfo.getSex()).index;
        if (basicUserInfo.getSign() != null) this.signature = basicUserInfo.getSign();
        if (basicUserInfo.getBackgroundPhoto() != null) this.backgroundPicture = basicUserInfo.getBackgroundPhoto();
        if (basicUserInfo.getPhoto() != null) this.avatar = basicUserInfo.getPhoto();
        if (basicUserInfo.getUserName() != null) this.username = basicUserInfo.getUserName();
    }
}